<?php
	include_once "dataservice.php";
	
	class binhluan_db
	{
		public function Them($MaBaiViet, $MaNguoiDung, $NoiDungBinhLuan, $ThoiGian, $MaTrangThai, $PIN)
		{
			$sql = "INSERT INTO `tbl_binhluan`(`MaBaiViet`, `MaNguoiDung`, `NoiDungBinhLuan`, `ThoiGian`, `MaTrangThai`, `PIN`) 
					VALUES ('$MaBaiViet', '$MaNguoiDung', '$NoiDungBinhLuan', '$ThoiGian', '$MaTrangThai', '$PIN')";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhat($MaBinhLuan, $MaBaiViet, $MaNguoiDung, $NoiDungBinhLuan, $ThoiGian, $MaTrangThai, $PIN)
		{
			$sql = "UPDATE 	`tbl_binhluan` 
					SET 	`MaBaiViet`       = '$MaBaiViet',
							`MaNguoiDung`     = '$MaNguoiDung',
							`NoiDungBinhLuan` = '$NoiDungBinhLuan',
							`ThoiGian`        =' $ThoiGian',
							`MaTrangThai`     = '$MaTrangThai', 
							`PIN`             = '$PIN'
					WHERE 	`MaBinhLuan` = $MaBinhLuan";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function Xoa($MaBinhLuan)
		{
			$sql = "DELETE FROM `tbl_binhluan` WHERE `MaBinhLuan` = '$MaBinhLuan'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaBinhLuan, $MaBaiViet, $MaNguoiDung, $MaTrangThai)
		{
			$sql = "SELECT * FROM `tbl_binhluan` WHERE 1 ";
			
			if($MaBinhLuan != 0)
				$sql .= " AND MaBinhLuan = '$MaBinhLuan'";
			if($MaBaiViet != 0)
				$sql .= " AND MaBaiViet = '$MaBaiViet'";
			if($MaNguoiDung != 0)
				$sql .= " AND MaNguoiDung = '$MaNguoiDung'";
			if($MaTrangThai != -1)
				$sql .= " AND MaTrangThai = '$MaTrangThai'";
			$sql .= " ORDER BY `ThoiGian` DESC";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function LayDanhSachPhanTrang($Start, $Limit, $MaBinhLuan, $MaBaiViet, $MaNguoiDung, $MaTrangThai)
		{
			$sql = "SELECT * FROM `tbl_binhluan` WHERE 1 ";
			if($MaBinhLuan != 0)
				$sql .= " AND MaBinhLuan = '$MaBinhLuan'";
			if($MaBaiViet != 0)
				$sql .= " AND MaBaiViet = '$MaBaiViet'";
			if($MaNguoiDung != 0)
				$sql .= " AND MaNguoiDung = '$MaNguoiDung'";
			if($MaTrangThai != -1)
				$sql .= " AND MaTrangThai = '$MaTrangThai'";
			$sql .= " ORDER BY `ThoiGian` DESC LIMIT $Start, $Limit";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function LayDanhSachTheoTrangThaiBaiViet($MaTrangThaiBaiViet, $MaTrangThaiBinhLuan)
		{
			$sql = "SELECT 	BL.* 
					FROM 	tbl_binhluan BL, tbl_baiviet BV 
					WHERE 	BL.MaBaiViet = BV.MaBaiViet AND 
							BV.MaTrangThai = '$MaTrangThaiBaiViet' ";
			if($MaTrangThaiBinhLuan != -1)
				$sql .= " AND BL.MaTrangThai = '$MaTrangThaiBinhLuan' ";
							
			$sql .= " ORDER BY BL.ThoiGian DESC ";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
		public function DemSoBinhLuanTheoMaNguoiDung($MaNguoiDung = "")
		{
			$sql = "SELECT COUNT(*) FROM  `tbl_binhluan` 
					WHERE  `MaNguoiDung`='$MaNguoiDung'";
					
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
	}
?>